﻿using Ken_WPF_UI.Data;
using Ken_WPF_UI.Model;
using Ken_WPF_UI.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Ken_WPF_UI.View
{
    /// <summary>
    /// MenuWindow2.xaml 的交互逻辑
    /// </summary>
    public partial class MenuWindow2 : Window
    {
        public MenuWindow2()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            List<MenuInfo> lists = MenuData.GetAllMenus();
            List<MenuItemModel> menuItemModels = new List<MenuItemModel>();
            AddAllMenus(lists, menuItemModels, null, 0);

            MenuViewModel viewModel = new MenuViewModel();
            viewModel.MenuList = menuItemModels;
            this.DataContext = viewModel;
        }

        /// <summary>
        /// 组合成菜单
        /// </summary>
        /// <param name="AllMenus"></param>
        /// <param name="menuItemModels"></param>
        /// <param name="pMenu"></param>
        /// <param name="parentId"></param>
        private void AddAllMenus(List<MenuInfo> AllMenus, List<MenuItemModel> menuItemModels, MenuItemModel pMenu, int parentId)
        {
            //匹配的父节点
            var subList = AllMenus.Where(m => m.ParentId == parentId);

            MenuItemModel modelMenu;
            foreach (var mi in subList)
            {
                modelMenu = new MenuItemModel();
                modelMenu.MenuName = mi.MenuName;
                modelMenu.MKey = mi.MKey;
                modelMenu.MenuId = mi.MenuId;
                modelMenu.Foreground = mi.Foreground;

                if (pMenu != null)
                {
                    pMenu.SubItems.Add(modelMenu);
                }
                else
                {
                    menuItemModels.Add(modelMenu);
                }
                AddAllMenus(AllMenus, menuItemModels, modelMenu, modelMenu.MenuId);
            }
        }



    }

    /// <summary>
    /// 菜单视图模型
    /// </summary>
    public  class MenuViewModel
    {
        public List<MenuItemModel> MenuList { get; set; }
    }
}
